(function(win) {
	axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
	// 创建axios实例
	const service = axios.create({
		// axios中请求配置有baseURL选项，表示请求URL公共部分
		// baseURL: 'http://localhost:80/apis',
		baseURL: 'http://localhost:8080',
		// 超时
		timeout: 1000000
	})
	// request拦截器
	service.interceptors.request.use(config => {
		// 是否需要设置 token
		// 让每个请求携带自定义token 请根据实际情况自行修改
		var admin = window.localStorage.getItem("adminId")
		if (admin != null && admin != '') {
			config.headers['admin'] = admin
		}

		// get请求映射params参数
		if (config.method === 'get' && config.params) {
			//localhost:8080/user/login?username=zhangsan&password=123456
			let url = config.url + '?';
			for (const propName of Object.keys(config.params)) {
				const value = config.params[propName];
				var part = encodeURIComponent(propName) + "=";
				if (value !== null && typeof(value) !== "undefined") {
					if (typeof value === 'object') {
						for (const key of Object.keys(value)) {
							let params = propName + '[' + key + ']';
							var subPart = encodeURIComponent(params) + "=";
							url += subPart + encodeURIComponent(value[key]) + "&";
						}
					} else {
						url += part + encodeURIComponent(value) + "&";
					}
				}
			}
			url = url.slice(0, -1);
			config.params = {};
			config.url = url;
		}
		return config
	}, error => {});

	/*
	 *1. 响应拦截器
	 *  use   200
	 * error  4xx
	 * 后台访问一个不存在的mapper  404
	 *
	 * */

	// 响应拦截器
	service.interceptors.response.use(res => {
			console.log('---响应拦截器---', res);
			console.log(service);
			//未设置状态码则默认=成功状态
			const code = res.data.code;
			//获取错误信息
			const msg = res.data.msg;
			console.log('---code---', code);
			if (res.data.code == 0 && res.data.msg == "用户未登录或者登录过期...") { // 返回登录页面
				localStorage.removeItem('adminId')
				localStorage.removeItem('userInfo');
				window.top.location.href = '../back/page/login/login.html'
			} else {
				return res.data
			}
		},
		error => {
			console.log('err' + error);
			let {
				message
			} = error;
			var code = message.substr(message.length - 3);
			if (code == 401) {
				// console.log(code + "权限不够跳转到登录页面")
				window.ELEMENT.Message({
					message: code + "权限不够跳转到登录页面",
					type: 'error',
					duration: 2 * 1000
				})
				window.setTimeout(function() {
					window.top.location.href = '../login/login.html'
				}, 1000)
			}
			if (code == 404) {
				console.log(code + "功能尚未完善")
				window.ELEMENT.Message({
					message: code + "功能尚未完善",
					type: 'error',
					duration: 5 * 1000
				})
			}
			return Promise.reject(error)
		}
	)
	// 把axios定义为全局变量
	window.$axios = service
	// 把minio文件访问网址定义成全局变量
	window.$minioUrl = "http://127.0.0.1:9000/jiayunplus/"
	// 图片上传网址
	window.$fileUpload = "http://localhost:8080/common/upload"
})(window);